import requests
import pandas as pd
import numpy as np
import zipfile
#creat map
import folium
# Matplotlib and associated plotting modules
import matplotlib.cm as cm
import matplotlib.colors as colors
Metro Vancouver is expected to accommodate 1 million population and 500,000 jobs by 2040. If all the new residents adopt a car-oreiented lifestyle, the regional road network will be heavily congested. The congestion translates into direct economic loss because people are wasting time sitting in the traffic. A resilient transit network is the solution - it provides an affordable and equitable mobility option to all the residents. Compared to a private car, a bus is able to move more people to their destination. In addition, the rapid transit network such as SkyTrain and Rapid Bus with dedicated right of way - this can reduce the transit rider's travel time significantly compared to the auto driver during the rush hour.
TransLink, the transit agency serving Metro Vancouver area, relies on the fare reveune to maintain financial sustainability and to reserve fund for transit infrastructure investments. In particular, the SkyTrain guideway and stations are expensive - a recent business case for Surrey-Langley SkyTrain Project estimated a cost $1.63 billion for extending the guideway by seven kilometres and constructing four new stations.
To learn from the existing operations, it would be helpful to characterize the existing rapid transit stations and understand the factors contributing to the high-ridership. This analysis will provide transit agency insights into correlation between the transit stop characteristics and ridership.
# load 2017 SkyTrain Station Rank and Average Weekday Boarding Data
# table I in 2017_TSPR_Summary.pdf, manually grab the data into the csv file
StationBoardings = pd.read_csv("ProjectData/2017SkyTrainStationRank.csv")
StationBoardings.head()
# the GTFS zipfile is loaded into GTFS: a dictionary of dataframes
gtfs_zip = zipfile.ZipFile("ProjectData/google_transit.zip")
GTFS = {}
for table in gtfs_zip.namelist():
table_name = table.split(".")[0]
GTFS[table_name] = pd.read_csv(gtfs_zip.open(table))
print(GTFS.keys())
# 8916 stops
# each skytrain platform has a parent_station
print(GTFS["stops"].loc[GTFS["stops"]["parent_station"]==99917])
print("---------------------------------------")
print(GTFS["stops"].loc[GTFS["stops"]["stop_id"]==99917])
# we are going to generate two station lists
# 1. existing rapid transit stations (parent_station): skytrain, seabus and west cost express
# 2. exiting bus stops
RapidTransitStop_list = GTFS["stops"]["parent_station"].dropna().unique().tolist()
print("%d rapid transit stations"%len(RapidTransitStop_list))
RapidTransitStop_df = GTFS["stops"].loc[GTFS["stops"]["stop_id"].isin(RapidTransitStop_list)]
print(RapidTransitStop_df.head())
print(RapidTransitStop_df.shape)
#bus stops are rows where parent_station is NaN and stop_id not in the RapidTransitStop_list
BusStop_filter = ~GTFS["stops"]["stop_id"].isin(RapidTransitStop_list)
BusStop_filter = BusStop_filter & (GTFS["stops"]["parent_station"].isnull())
BusStop_df = GTFS["stops"].loc[BusStop_filter]
print("%d bus stops"%len(BusStop_df))
print(BusStop_df.head())
print(BusStop_df.shape)
# show rapid transit station locations on map
# map centre lat,long
from geopy.geocoders import Nominatim # convert an address into latitude and longitude values
address = 'New Westminster, BC'
geolocator = Nominatim(user_agent="ny_explorer")
location = geolocator.geocode(address)
latitude = location.latitude
longitude = location.longitude
# create map of rapid transit stations using latitude and longitude values
Station_Map = folium.Map(location=[latitude, longitude], zoom_start=11)
# add bus stop markers to map
for lat, lng, stopid, stopname in zip(BusStop_df['stop_lat'], BusStop_df['stop_lon'], BusStop_df['stop_id'], BusStop_df['stop_name']):
label = '{}, {}'.format(stopid, stopname)
label = folium.Popup(label, parse_html=True)
folium.CircleMarker(
[lat, lng],
radius=1,
popup=label,
color='#9b99c4',
fill=True,
fill_color='#9b99c4',
fill_opacity=0.1,
parse_html=False).add_to(Station_Map)
# add rapid transit markers to map
for lat, lng, stopid, stopname in zip(RapidTransitStop_df['stop_lat'], RapidTransitStop_df['stop_lon'], RapidTransitStop_df['stop_id'], RapidTransitStop_df['stop_name']):
label = '{}, {}'.format(stopid, stopname)
label = folium.Popup(label, parse_html=True)
folium.CircleMarker(
[lat, lng],
radius=5,
popup=label,
color='blue',
fill=True,
fill_color='#3186cc',
fill_opacity=0.9,
parse_html=False).add_to(Station_Map)
Station_Map